package com.xgj.ph.servlet;

import com.alibaba.fastjson.JSON;
import com.xgj.ph.dao.ShopDao;
import com.xgj.ph.dao.impl.ShopDaoImpl;
import com.xgj.ph.entity.Shop;
import com.xgj.ph.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    ShopDao shopDao = new ShopDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  1.设置编码方式
        //  2.读取action，判断请求
        String action = req.getParameter("action");
        //  3.根据action执行
        if("selectAll".equals(action)){
            //  3.1接收数据 (从前端)
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String communityId = req.getParameter("communityId");
            //  3.2 封装数据到map集合（便于发给后端）
            Map<String,String> map = new HashMap<>();
            map.put("communityId", communityId);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("shopName",shopName);
            map.put("shopPhone",shopPhone);
            //  3.3 连接到数据库操作
            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            //  3.4 做出响应    (将list status total再次封装成map)
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            //  用json格式显示，检查是否成功获取数据
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("selectShopAll".equals(action)){
            //  3.1接收数据 (从前端)
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            //  3.2 封装数据到map集合（便于发给后端）
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            //  3.3 连接到数据库操作
            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            //  3.4 做出响应    (将list status total再次封装成map)
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            Result result = Result.success(resultMap);
            //  用json格式显示，检查是否成功获取数据
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("update".equals(action)){
            String doIt = req.getParameter("doIt"); //  接收前端传过来的是添加请求还是修改请求
            String id = req.getParameter("shopId");
            int shopId = 0;
            if(id!=null && !"".equals(id)){
                shopId = Integer.parseInt(id);
            }
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopAddress = req.getParameter("shopAddress");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            //  组成对象
            Shop shop = new Shop(shopId,shopName,shopPhone,shopAddress,shopDescribe,shopProvince,shopCity,shopDistrict,shopLatitude,shopLongitude);
            if("addShop".equals(doIt)){     //  执行增加操作
                int num = shopDao.insert(shop);
                Result result = null;
                if(num!=0){
                    result = Result.success("新增成功");
                }else{
                    result = Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else{                          //  执行修改操作
                int num = shopDao.update(shop);
                Result result = null;
                if(num!=0){
                    result = Result.success("修改成功");
                }else{
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if("delete".equals(action)){  //执行删除操作
            String id =req.getParameter("shopId");
            int shopId = 0;
            if(id!=null && !"".equals(id)){
                shopId = Integer.parseInt(id);
            }
            int num = shopDao.delete(shopId);
            Result result = null;
            if(num!=0){
                result = Result.success("删除成功");
            }else{
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
